<?php

/**
 * Class for viewing a grouped CSS selector.
 *
 * @author Ian Clarence ian.clarence@gmail.com
 */ 
class ViewGroupedSelector {
    
    /**
     * Array of objects of type ViewNestedSelector. 
     */
    private $selectors = array();

    /**
     * The constructor.
     *
     * @param array $selectors
     */
    public function __construct(array $selectors = array()) {      
        if (count($selectors > 0)) {
            foreach ($selectors as $selectorKey => $selector) {
                if ($selector instanceof ViewNestedSelector) {
                    $this->selectors[$selectorKey] = $selector;
                }
            }
        }
    }

    /**
     * Returns a simple grouped CSS selector.
     *
     * @param string $tag
     * @param string $id
     * @param string $className
     * @param string $pseudoClass
     * @return ViewGroupedSelector
     */
    public static function groupedSelectorInit(
        $tag = NULL, 
        $id = NULL, 
        $className = NULL, 
        $pseudoClass = NULL
    ) {
        return new self(array(ViewNestedSelector::nestedSelectorInit($tag, $id, $className, $pseudoClass)));
    }
    
    /**
     * Parses a grouped selector string, and creates object.
     * Could be done better with regular expressions, I know!
     * 
     * @param string $groupedSelectorString
     * @return ViewGroupedSelector
     */
    public static function selectorParser($groupedSelectorString) {
        $nestedSelectorStrings = explode(',', $groupedSelectorString);
        $nestedSelectors = array();
        
        /*
         * Looping through the grouped selector string.
         */
        foreach ($nestedSelectorStrings as $nestedKey => $nestedSelectorString) {
            $selectorStrings = explode(' ', trim($nestedSelectorString));
            $selectors = array();
            
            /**
             * Looping through the nested selector strings. 
             */
            foreach ($selectorStrings as $key => $selectorString) {
                
                /**
                 * Finding pseudo-classes. 
                 */
                $getPseudoClass = explode(':', $selectorString);
                $pseudoClass = isset($getPseudoClass[1]) ? $getPseudoClass[1] : NULL;
                
                /**
                 * Finding the id and the tag. 
                 */
                $getId = explode('#', $getPseudoClass[0]);
                $containsId = isset($getId[1]) ? $getId[1]: NULL;
                $containsTag = $getId[0];
                $tag = NULL;
                $id = NULL;
                
                /**
                 * Finding the array of classes. 
                 */
                $classes = array();
                $getClasses = explode('.', $containsId);
                if (count($getClasses) > 1) {
                    foreach ($getClasses as $getClassesKey => $getClass) {
                        if ($getClassesKey == 0) {
                            $id = $getClass == '' ? NULL : $getClass;
                        }
                        else {
                            $classes[] = $getClass == '' ? NULL : $getClass;
                        }
                    }
                }
                else { 
                    $id = $containsId == '' ? NULL : $containsId;                   
                    $getClasses = explode('.', $containsTag);
                    if (count($getClasses) > 1) {
                        foreach ($getClasses as $getClassesKey => $getClass) {
                            if ($getClassesKey == 0) {
                                $tag = $getClass == '' ? NULL : $getClass;
                            }
                            else {
                                $classes[] = $getClass == '' ? NULL : $getClass;
                            }
                        }
                    }
                    else {
                        $tag = $containsTag == '' ? NULL : $containsTag;                        
                    }
                }
                
                /**
                 * Building the selector object. 
                 */
                $selectors[$key] = new ViewSelector(array(
                    'tag' => $tag,
                    'element_id' => $id,
                    'class_array' => $classes,
                    'pseudo_class' => $pseudoClass
                ));
            }
            
            /**
             * Building the nested selector object. 
             */
            $nestedSelectors[$nestedKey] = new ViewNestedSelector($selectors);            
        }
        
        /**
         * Returning the grouped selector object. 
         */
        return new self($nestedSelectors);
    }
    
    /**
     * Viewing the grouped selector.
     *
     * @return string
     */
    public function view() {
        if (count($this->selectors) == 0) {
            return '';
        }
        $output = array();
        foreach ($this->selectors as $selector) {
            $output[] = $selector->view();
        }
        return implode(",\n", $output) . "\n";
    }
    
}